import { ref } from 'vue';

//  年
const year = ref('2025') as any;

function setYear(value: any) {
    year.value = getValue('year', value);
}


export function useYear(): any[] {
    return [ year, setYear ];
}

const yearLists = ref([]) as any;

function setYearList(data: any = []) {
    yearLists.value = data.map((_s: any ) => {
        return _s.text.replace('年', '');
    }).reverse();

    //  每次获取顺带着更新 year 让他保持最新的状态
    year.value =  yearLists.value[0];
}

export function useYearList(): any[] {
    return [ yearLists, setYearList ]; 
}


//  用户信息
const userInfo = ref({}) as any;

function setUserInfo(value: any) {;
    userInfo.value = value;
}

export function useUserInfo(): any[] {
    return [ userInfo, setUserInfo ];
}


//  判断用户是否是管理员
const isAdmin = ref(false);
function setAdmin(data: any) {
    isAdmin.value = data;
}

export function useAdmin(): any {
    return [ isAdmin, setAdmin ];
}


/* 
    @param key 关键字
    @param default_data 默认值
    @return 通过key 从 storage 中获取的数据
*/
function getValue(key: string = '', default_data: any = ''): any {
    setValue(key, default_data);
    return uni.getStorageSync(key);
}

//  设置
function setValue(key: string = '', value: any = '') {
    uni.setStorageSync(key, value);
}
